# -*- coding: utf8 -*-
import logging
import re
import time
import common

site_index = 'joysound'
site_keyword = 'joysound'
site_url = 'http://joysound.com/'
test_url = 'http://joysound.com/ex/search/karaoke/_selSongNo_28721_songwords.htm'

class Joysound:
    def __init__(self):
        self.lyric = None
        self.song_info = None
        self.cookie = []

        self.read_config()

    def read_config(self):
        try:
            import config
            cfg = config.Config()
            self.id = cfg.get('joysound', 'id')
            self.password = cfg.get('joysound', 'password')
        except:
            self.id = ''
            self.password = ''

    def get_lyric(self, url):
        # 1. get necessary cookie by login
        self.do_login()
        
        # get cookie from CSIE
#         self.get_cookie_from_csie()

        # 2. retrieve lyric page
        sdata = self.get_sdata(url)

        # 3. retrieve real lyric content through AMF protocol
        raw_lyric = self.get_raw_lyric(sdata)

        # 4. parse raw lyric
        lyric = self.parse_raw_lyric(raw_lyric)

#         # get lyric from CSIE
#         lyric = self.get_lyric_from_csie(url)
        
        return lyric

    def get_lyric_from_csie(self, lyric_url):
        csie_url = 'http://w.csie.org/~b91072/php/lyric_get_helper/site_joysound.php'

        url = '%s?time=%.0f&url=%s' % (csie_url, time.time(), lyric_url, )

        handle = self.send_request(url)

        logging.warning(url)

        lyric = handle.get_content()

        lyric = lyric.decode('utf8').strip()

        return lyric

    def do_login(self):
        """
        Login Joysound.com, 
        get 2 important Cookie values and save it in self.cookie
        """
        url = 'http://joysound.com/ex/utasuki/forwardLogin.htm'
        data = 'loginId=%s&loginPass=%s' % (self.id, self.password, )
        headers = {}

        handle = self.send_request(url, data, headers)
        if not handle:
            # something error
            logging.info('damn, login failed')
            

        headers = handle.get_info()

        cookie = self.get_cookie(headers)
        self.cookie = ';'.join(cookie)

        logging.debug('headers=%s' % (str(headers)))
        logging.debug('self.cookie=%s' % (self.cookie))

    def get_cookie_from_csie(self):
        # get login cookie from CSIE
        url = 'http://w.csie.org/~b91072/php/lyric_get_helper/get_joysound_cookie.php'

        handle = self.send_request(url)
        json = handle.get_content()

        pattern = '"([^"]+)"'
        obj = re.search(pattern, json)
        if obj:
            self.cookie = obj.group(1)
            
        
    def get_cookie(self, headers):
        cookie = []

        if 'set-cookie' in headers:
            raw_cookie = headers['set-cookie']

            pattern = '(JSESSIONID=[^;]+)'
            obj = re.compile(pattern).search(raw_cookie)
            if obj:
                cookie.append(obj.group(1)) 
                
            pattern = '(AlteonP=[^;]+)'
            obj = re.compile(pattern).search(raw_cookie)
            if obj:
                cookie.append(obj.group(1)) 
        else:
            raise

        return cookie

    
    def get_sdata(self, url):
        """
        Load the Lyric Page
        find the sdata for this lyric
        """
        data = None
        headers = {
            'Cookie': self.cookie,
        }

        handle = self.send_request(url, data, headers)
        
        html = handle.get_content()

        # do parsing
        sdata = None
        pattern = 'viewLyrics\.swf\?sd=([^"]+)'
        regex = re.compile(pattern).search(html)
        if regex:
            sdata = regex.group(1)

        logging.debug('sdata=%s' % (str(sdata)))

        return sdata

    def get_raw_lyric(self, sdata):
        """
        With sdata, load the lyric
        Action Message Format
        """
        if sdata == None:
            return None

        prefix = '\0\0\0\0\0\1\0\x1a'
        url = 'http://www.lyriget.jp/flashservices/gateway'
        data = '%s%s\0\2/1\0\0\0\x33\x0a\0\0\0\1\3\0\5sData\2\0 %s\0\0\x09' % (
            prefix, 'xing_cfc.songdata.get_data', sdata
        )
        headers = {
            'Content-Type': 'application/x-amf',
        }

        handle = self.send_request(url, data, headers)

        return handle.get_content()
    
    def parse_raw_lyric(self, raw_lyric):
        if raw_lyric == None:
            return None

        # song info
        artist = self.get_song_info('KNM', raw_lyric)
        title = self.get_song_info('MNM', raw_lyric)
        lyricist = self.get_song_info('SINM', raw_lyric)
        music = self.get_song_info('SKNM', raw_lyric)
        lyric = self.get_song_info('KAS', raw_lyric)

        lines = []

        lines.append(u'%s\n' % (title))
        lines.append(u'%s：%s' % (u'歌手', artist))
        lines.append(u'%s：%s' % (u'作詞', lyricist))
        lines.append(u'%s：%s' % (u'作曲', music))
        lines.append('\n\n')

        song_info = '\n'.join(lines)

        lyric = song_info + lyric

        return lyric

    def get_song_info(self, prefix, raw_lyric):
        start = raw_lyric.find(prefix) + len(prefix) + 3
        end = raw_lyric.find('\0', start)
        result = raw_lyric[start:end].decode('utf8')

        return result

    def send_request(self, url, data=None, headers=None):
        obj = common.URL(url, data, headers)
        return obj

def get_lyric(url):
    js = Joysound()

    lyric = js.get_lyric(url)

    return lyric

def _get_song_info(url):
    pass

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    url = test_url
    print url
    lyric = get_lyric(url)
    print repr(lyric) 

